define(function () {
    var self,content;

    function ysxxhb() {
        self = this;
        self.mData = {
            productSetItem: null
        };
        self.imgArr = [];
        self.init();
    }

    //根据车牌、司机名字、手机号 获取司机信息
    ysxxhb.prototype.getinfo3 = function () {
        app.ajax({
            xurl: "api/wx/gwat500/getGwaaData2",
            xdata: {
                GWCU008:content.form.GWCU008,//车牌
                GWCU009:content.form.GWCU009,//司机
                GWCU010:content.form.GWCU010//手机号
            },
            cb: function (result) {
                if (result.ok) {
                    if(result.data.data.length>0){
                        content.form.GWCU008 = result.data.data[0].GWCU008;
                        content.form.GWCU009 = result.data.data[0].GWCU009;
                        content.form.GWCU010 = result.data.data[0].GWCU010;
                    }

                }
            }
        });
    };

    //根据微信号获取司机信息
    ysxxhb.prototype.getinfo1 = function () {
        content.form.GWCU009 = app.cookie('_WX_USERNAME_');
        content.form.GWCU010 = app.cookie('_WX_MOBILE_');
        app.ajax({
            xurl: "api/wx/gwat500/getGwaaData",
            xdata: {
                GWAA004:content.form.GWCU010//司机 手机号
            },
            cb: function (result) {
                if (result.ok) {
                    if(result.data.data.length>0){
                        content.form.GWCU008 = result.data.data[0].GWCU008;

                    }

                }
            }
        });
    };

    //根据序列号获取信息 添加到明细表中
    ysxxhb.prototype.getinfo2 = function () {
        var product = self.mData.productSetItem;
        var info = content.form.info;
        app.ajax({
            xurl: "api/wx/gwat500/getGwcsData",
            xdata: {
                page: product.page+1,
                limit: product.limit,
                GWCS019:content.GWCS019//序列号

            },
            cb: function (result) {
                if (result.ok) {

                    if(result.data.data.length == 0){
                       // app.toast('添加失败');
                    }else{
                        for(var i=0;i<info.length;i++){
                            if(info[i].GWCS019 == result.data.data[0].GWCS019){
                                info.splice(i,1);
                            }

                        }
                        info.pushArray(result.data.data,false);
                        app.toast('添加成功');
                        content.GWCS019 = ''; //添加完成，清空输入框
                    }
                }
            }
        });
    };

    ysxxhb.prototype.initVue = function () {
        content = new Vue({
            el: app.getByid(doc, "content"),
            data: function () {
                return {
                    form:{
                        GWCU008:'',//车牌
                        GWCU009:'',//司机
                        GWCU010:'',//手机号
                        GWCU017:'',//运输照片地址
                        info: []//列表
                    },
                    GWCS019:'',//序列号
                    imgs: self.imgArr
                }
            },
            watch: {
                'form.GWCU008':function () { //车牌
                    setTimeout(function () {
                        self.getinfo3();
                    }, 500)

                },
                'form.GWCU009':function () { //司机
                    setTimeout(function () {
                        self.getinfo3();
                    }, 500)
                },
                'form.GWCU010':function () { //手机号
                    setTimeout(function () {
                        self.getinfo3();
                    }, 500)
                },
                GWCS019:function () { //监听序列号变化，添加信息到列表
                    if(content.GWCS019!=''){
                        self.getinfo2();
                    }
                }
            },
            methods: {
                btn_gwcs019:function () {
                    var val = this;
                    app.scanQRCode(function(result){
                        Vue.set(content,'GWCS019',result);
                    });
                },
                delimg: function (i) {
                    self.imgArr.removei(i);
                },
                addImgs: function () {
                    app.chooseImageNew(function (imgs) {
                        //self.imgArr.pushArray(imgs);
                        //app.extend(self.imgArr, imgs);
                        for (var i = 0; i < imgs.length; i++) {
                            self.imgArr.push({
                                img: imgs[i].img,
                                id: imgs[i].localId
                            })
                        }
                    });
                }
            }
        });
    };

    ysxxhb.prototype.init = function () {
        self.initVue();
        self.getinfo1();

        mui.ready(function () {
            if (!self.mData.productSetItem) {
                self.mData.productSetItem = {
                    page: 0,
                    limit: 15,
                    start: function () {
                        return (this.page) * this.limit;
                    }
                };
            }

            mui('.mui-scroll-wrapper').scroll({
                deceleration: 0.0005
            });

            app.taps.call(win, [
                {
                    o: 'submitDiv',
                    cb: function () {
                        if (content.form.GWCU008 == '') {
                            app.toast('车牌不能为空');
                            return;
                        }
                        if (content.form.GWCU009 == '') {
                            app.toast('司机姓名不能为空');
                            return;
                        }
                        if (content.form.GWCU010 == '') {
                            app.toast('司机电话不能为空');
                            return;
                        }

                       app.showLoading();
                       app.uploadLocalIds({
                            ids:self.imgArr,
                            cb:function (imgs) {
                                //content.form.GWCU017 = imgs.join(',');
                                var imgurl = "";
                                for (var i = 0; i < imgs.length; i++) {
                                    imgurl += (imgs[i].name + ",");
                                }
                                content.form.GWCU017=imgurl.substring(0,imgurl.length-1);
                                var _data = {
                                    GWCU008:content.form.GWCU008,
                                    GWCU009:content.form.GWCU009,
                                    GWCU010:content.form.GWCU010,
                                    GWCU017:content.form.GWCU017,
                                    CHARACTERISTICS:[]
                                }
                                var info = content.form.info;
                                for(var i = 0; i<content.form.info.length;i++){
                                    _data.CHARACTERISTICS.push({
                                        GWCU001:info[i].GWCS001, //产品编号
                                        GWCU002:info[i].GWCS019,//产品序列号
                                        GWCU003:info[i].GWCS002,//销售单位
                                        GWCU004:info[i].GWCS003,//运输数量
                                        GWCU012:info[i].GWAKL004,//收货人
                                        GWCU013:info[i].GWCS020,//收货地址
                                        GWCU014:info[i].GWCS014,//销售商联系人
                                        GWCU015:info[i].GWCS015,//销售商联系人电话
                                        GWCU030:info[i].GWCSDOCNO,//正式订单ID
                                        GWCU031:info[i].GWCSSEQ,//正式订单  相序
                                    })
                                }
                                app.ajax({
                                    xurl: "api/wx/gwat500/saveData",
                                    loading: true,
                                    xdata: {
                                        obj:app.jsonString(_data)
                                    },
                                    cb: function (result) {
                                        app.hideLoading();
                                        if (result.ok) {
                                            app.toast('提交成功！');
                                            setTimeout(function () {
                                                mui.back();
                                            }, 600);
                                        } else {
                                            app.toast(result.data.message);
                                        }
                                    }
                                });
                            }
                        });

                    }
                },
            ]);
        })
    };


    return new ysxxhb();
})